package network.tcp;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by Administrator on 2015/8/7.
 */
public class TcpServer {
    private final static int PORT = 9999;

    public static void main(String[] args) throws Exception {
        try {
            startHttpServer(PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void startHttpServer(int port) throws Exception {
        ServerSocket ss = new ServerSocket(PORT);
        System.out.println("启动服务器....");
        while (!Thread.currentThread().isInterrupted()) {
            Socket client = ss.accept();

            byte[] bytes = new byte[1024];
            //读取客户端发送来的消息
            while (client.getInputStream().read(bytes) != -1) {
                System.out.println("客户端请求信息：" + new String(bytes) );
            }
            //给客户端响应
            OutputStream out=client.getOutputStream();
            out.write("hello client".getBytes());
            out.flush();
            client.close();
        }
    }

}
